<?php
if(!defined('IN_INDEX')) 
 { 
  header('Location: index.php');
  exit;
 }

// upload folder:
$uploaded_images_path = 'images/uploaded/';
$images_per_page = 5;

if(($settings['upload_images']==1 && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0)||($settings['upload_images']==2 && isset($_SESSION[$settings['session_prefix'].'user_id']))||($settings['upload_images']==3))
 {
  if(isset($_FILES['probe']) && $_FILES['probe']['size'] != 0 && !$_FILES['probe']['error'])
   {
    unset($errors);
    $image_info = getimagesize($_FILES['probe']['tmp_name']);

    if(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3) $errors[] = 'invalid_file_format';

    if(empty($errors))
     {
      if($_FILES['probe']['size'] > $settings['upload_max_img_size']*1000 || $image_info[0] > $settings['upload_max_img_width'] || $image_info[1] > $settings['upload_max_img_height'])
       {
        #$compression = 10;
        $width=$image_info[0];
        $height=$image_info[1];
        
        // resize if too large:
        if($width > $settings['upload_max_img_width'] || $height > $settings['upload_max_img_height'])
         {
          if($width >= $height)
           {
            $new_width = $settings['upload_max_img_width'];
            $new_height = intval($height*$new_width/$width);
           }
          else
           {
            $new_height = $settings['upload_max_img_height'];
            $new_width = intval($width*$new_height/$height);
           }
         }        
        else
         {
          $new_width=$width;
          $new_height=$height;
         }
        
        $img_tmp_name = uniqid(rand()).'.tmp';

        for($compression = 100; $compression>1; $compression=$compression-10)
         {
          if(!resize_image($_FILES['probe']['tmp_name'], $uploaded_images_path.$img_tmp_name, $new_width, $new_height, $compression)) 
           { 
            $file_size = $_FILES['probe']['size']; // @filesize($_FILES['probe']['tmp_name']); 
            break; 
           }
          $file_size = @filesize($uploaded_images_path.$img_tmp_name);
          if($image_info[2]!=2 && $file_size > $settings['upload_max_img_size']*1000) break;
          if($file_size <= $settings['upload_max_img_size']*1000) break;
         }
        if($file_size > $settings['upload_max_img_size']*1000) 
         {
          $smarty->assign('width',$image_info[0]);
          $smarty->assign('height',$image_info[1]);
          $smarty->assign('filesize',number_format($_FILES['probe']['size']/1000,0,',',''));
          $smarty->assign('max_width',$settings['upload_max_img_width']);
          $smarty->assign('max_height',$settings['upload_max_img_height']);
          $smarty->assign('max_filesize',$settings['upload_max_img_size']);
          $errors[] = 'file_too_large';
         } 
        if(isset($errors))
         {
          if(file_exists($uploaded_images_path.$img_tmp_name))
           {
            @chmod($uploaded_images_path.$img_tmp_name, 0777);
            @unlink($uploaded_images_path.$img_tmp_name);
           }
         }
       }
     }

    if(empty($errors))
     {
      $nr = 0;
      switch($image_info[2])
       {
        case 1:
         for(;;) { $nr++; if (!file_exists($uploaded_images_path."image".$nr.".gif")) break; }
         $filename = "image".$nr.".gif";
        break;
        case 2:
         for(;;) { $nr++; if (!file_exists($uploaded_images_path."image".$nr.".jpg")) break; }
         $filename = "image".$nr.".jpg";
        break;
        case 3:
         for(;;) { $nr++; if (!file_exists($uploaded_images_path."image".$nr.".png")) break; }
         $filename = "image".$nr.".png";
        break;
       }
      if(isset($img_tmp_name))
       {
        @rename($uploaded_images_path.$img_tmp_name, $uploaded_images_path.$filename) or $errors[] = 'upload_error';
        $smarty->assign('image_downsized',true);
        $smarty->assign('new_width',$new_width);
        $smarty->assign('new_height',$new_height);
        $smarty->assign('new_filesize',number_format($file_size/1000,0,',',''));
       }
      else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
    if(empty($errors))
     {
      @chmod($uploaded_images_path.$filename, 0644);
      $smarty->assign('uploaded_file',$filename);
     }
    else 
     {
      $smarty->assign('errors',$errors);
      $smarty->assign('form',true);
     } 
   }
  else 
   {
    if(empty($_GET['browse_images'])) $smarty->assign('form',true);
    else 
     {
      $browse_images = intval($_GET['browse_images']);
      if($browse_images<1)$browse_images=1;
      $handle=opendir($uploaded_images_path);
      while ($file = readdir($handle))
       {
        if(preg_match('/\.jpg$/i', $file) || preg_match('/\.png$/i', $file) || preg_match('/\.gif$/i', $file))
         {
          $images[] = $file;
         }
        }
      closedir($handle);
      if(isset($images))
       {
        $images_count = count($images);
        if($browse_images > ceil($images_count/$images_per_page)) $browse_images=ceil($images_count/$images_per_page);  
        $start = $browse_images * $images_per_page - $images_per_page;
        $show_images_to = $browse_images * $images_per_page;
        if($show_images_to>$images_count) $show_images_to = $images_count;
       }
      else $images_count = 0;
      if($browse_images*$images_per_page < $images_count) $smarty->assign('next',$browse_images+1);
      if($browse_images > 1) $smarty->assign('previous',$browse_images-1);
      $smarty->assign('browse_images',true);
      $smarty->assign('images_per_page',$images_per_page);
      if(isset($images)) $smarty->assign('images',$images);
      if(isset($start)) $smarty->assign('start',$start);
     } 
   }  
  
  if(empty($errors) && isset($_FILES['probe']['error'])) 
   {
    $smarty->assign('server_max_filesize', ini_get('upload_max_filesize'));
    $errors[] = 'upload_error_2';
    $smarty->assign('errors',$errors);
   }   
 
  }
 
$template = 'upload_image.tpl'; 
?>
